package com.xzw.cameratest.base;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;


import com.xzw.cameratest.R;

import java.lang.reflect.Field;

/**
 * @description:
 * @author:111144
 * @date:2020/5/20
 */
public abstract class BaseFragment extends Fragment {
    private ProgressDialog mProgressDialog;

    protected abstract View initView(LayoutInflater inflater, ViewGroup container);

    protected abstract void initData();

    protected abstract void initListener();


    /**
     * onDestroy()中资源释放，正常使用时子类重写该方法
     */
    protected void releaseOnDestroy() {
    }

    /**
     * onStop()中资源释放，正常使用时子类重写该方法
     */
    protected void releaseOnStop() {
    }

    /**
     * onPause()中资源释放，正常使用时子类重写该方法
     */
    protected void releaseOnPause() {
    }

    public Fragment mFragment;
    public int id;

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        mProgressDialog = new ProgressDialog(getActivity(), R.style.loading_style);
        mProgressDialog.setCanceledOnTouchOutside(false);
        initListener();
        initData();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mFragment = this;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        return initView(inflater, container);
    }


    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragment = Fragment.class.getDeclaredField("mChildFragmentManager");
            childFragment.setAccessible(true);
            childFragment.set(this, null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        releaseOnPause();
    }

    @Override
    public void onStop() {
        super.onStop();
        releaseOnStop();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        releaseOnDestroy();
    }

    protected <T> T $(View view, @IdRes int resid) {
        return (T) view.findViewById(resid);
    }


    protected void showToast(String str) {
        Toast.makeText(getActivity(), str, Toast.LENGTH_SHORT).show();
    }

    protected void showProgressDialog() {
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
            mProgressDialog.setContentView(R.layout.view_loading_layout);
        }
    }

    protected void showProgressDialog(String str) {
        if (mProgressDialog != null && !mProgressDialog.isShowing()) {
            mProgressDialog.show();
            mProgressDialog.setContentView(R.layout.view_loading_layout);
            TextView content = mProgressDialog.findViewById(R.id.tx_content);
            content.setText(str);
        }
    }

    public void dismissProgressDialog() {
        if (mProgressDialog != null && mProgressDialog.isShowing()) {
            mProgressDialog.dismiss();
        }
    }
}

